package com.zhangmeng.utils;

import com.zhangmeng.service.Server;

import java.util.Properties;

/**
 * @author zm
 * @date 2025/2/22 17:01
 * @version: 1.0
 */
public class Globalobj {
    private static Globalobj instance ;

    private Globalobj() {
    }

    private Server TcpServer;        //当前Zinx的全局Server对象
    private String Host;              //当前服务器主机IP
    private int TcpPort;         //当前服务器主机监听端口号
    private String Name;          //当前服务器名称
    private String Version;        //当前Zinx版本号

    private int MaxPacketSize; //都需数据包的最大值
    private int MaxConn;   //当前服务器主机允许的最大链接个数

    //    /读取用户的配置文件
    public static void Reload() {
        Properties prop = new Properties();
        try {
            prop.load(Globalobj.class.getResourceAsStream("/server.properties"));
            instance.Host = prop.getProperty("Host");
            instance.Name = prop.getProperty("Name");

            String tcpPort = prop.getProperty("TcpPort");
            if (tcpPort != null && !tcpPort.isEmpty()){
                instance.TcpPort = Integer.parseInt(tcpPort);
            }

            String version = prop.getProperty("Version");
            if (version != null && !version.isEmpty()){
                instance.Version = version;
            }
            String maxPacketSize = prop.getProperty("maxPacketSize");
            if (maxPacketSize != null && !maxPacketSize.isEmpty()){
                instance.MaxPacketSize = Integer.parseInt(maxPacketSize);
            }

            String maxConn = prop.getProperty("MaxConn");
            if (maxConn != null && !maxConn.isEmpty()){
                instance.MaxConn = Integer.parseInt(maxConn);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//       getInstance();
//        System.out.println(instance.getName());
//    }

    public static Globalobj getInstance() {

        //初始化GlobalObject变量，设置一些默认值
        instance = new Globalobj();
        instance.setName("ZinxServerApp");
        instance.setVersion("V0.4");
        instance.setTcpPort(7777);
        instance.setHost("0.0.0.0");
        instance.setMaxConn(12000);
        instance.setMaxPacketSize(4096);

        Reload();
        return instance;
    }

    public Server getTcpServer() {
        return TcpServer;
    }

    public void setTcpServer(Server tcpServer) {
        TcpServer = tcpServer;
    }

    public String getHost() {
        return Host;
    }

    public void setHost(String host) {
        Host = host;
    }

    public int getTcpPort() {
        return TcpPort;
    }

    public void setTcpPort(int tcpPort) {
        TcpPort = tcpPort;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getVersion() {
        return Version;
    }

    public void setVersion(String version) {
        Version = version;
    }

    public int getMaxPacketSize() {
        return MaxPacketSize;
    }

    public void setMaxPacketSize(int maxPacketSize) {
        MaxPacketSize = maxPacketSize;
    }

    public int getMaxConn() {
        return MaxConn;
    }

    public void setMaxConn(int maxConn) {
        MaxConn = maxConn;
    }
}
